<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<link rel="stylesheet" href="css/common.css"/>
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="css/jquery-ui-timepicker-addon.css"/>
<link rel="stylesheet" href="css/jquery-ui.css"/>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="css/city-picker.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/jquery-impromptu.min.css" type="text/css"/>
<link rel="stylesheet" href="css/combo.select.css" type="text/css"/>
<body>
<div id="dataLoad" style="display:none; position: fixed; width: 100%; top: 20%; z-index: 1001;">
       <table width=100% height=100% border=0 align=center valign=middle>
        <tr height=50%><td align=center>&nbsp;</td></tr>
        <tr><td align=center><img src="images/loading.gif"/></td></tr>
        <tr><td align=center>获取中，请稍后......</td></tr>
        <tr height=50%><td align=center>&nbsp;</td></tr>
       </table>
     </div>
    <div id="search_bar" class="mt10">
        <div class="box">
            <div class="box_border">
                <div class="box_top"><b class="pl15">数据筛选</b></div>
                <div class="box_center pt10 pb10" style="padding-left: 35%;">
                    <table class="form_table" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td>审批类型</td>
                            <td>
                                <span class="fl">
                                    <div class="select_border">
                                        <div class="select_containers ">
                                            <select name="" class="select" id="approve_type">

                                            </select>
                                        </div>
                                    </div>
                                </span>
                            </td>
                            <td class="section_area">部门选择</td>
                            <td class="section_area">
                                <span class="fl">
                                    <div class="select_border">
                                        <div class="select_containers ">
                                            <select name="" class="select" id="section_type">

                                            </select>
                                        </div>
                                    </div>
                                </span>
                            </td>
                            <td><input type="button" value="确定" id="confirm_query" class="ext_btn ext_btn_submit"></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="mt10">
        <div class="box">
            <div class="box_border">
                <div class="box_top"><b class="pl15">业务信息</b></div>
                <div class="mt10">
                    <div id="table" class="mt10">
                        <div class="box span10 oh" style="margin-left: auto; margin-right: auto">
                            <table id="section_app_info" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.5rem">
                                <tr>
                                    <th class="td_center" style="width:5%">编号</th>
                                    <th class="td_center" style="width:5%">申请人</th>
                                    <th class="td_center" style="width:10%">所属部门</th>
                                    <th class="td_center" style="width:15%">审批类型</th>
                                    <th class="td_center" style="width:15%">申请时间</th>
                                    <th class="td_center" style="width:15%">可选操作</th>
                                </tr>
                            </table>
                        </div>
                        <div class="page mt10">
                            <div class="pagination">
                                <ul>
                                    <li class="first-child" ><a href="#" id="firstPage">首页</a></li>
                                    <li class="" ><a href="#" id="prevPage">上一页</a></li>
                                    <li class="" ><a href="#" id="nextPage">下一页</a></li>
                                    <li class="last-child " ><a href="#" id="endPage">末页</a></li>
                                    <li class="disabled pagedata" id="page1"><a id="pageInfo">1/20</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/config.js"></script>
<script type="text/javascript" src="js/jquery-impromptu.min.js"></script>
<script src="js/jquery.combo.select.js" type="text/javascript"></script>
<script>
    //计数器，用于统计当前显示到第几条
    var curItemCount = 0;
    //全局变量，用于记录当前是报销审批还是业务审批
    var is_fee = 0;

    //辅助变量，保存当前获得的数据
    var curInfo;

    var firstSet = true;

    //辅助函数，处理搜索信息框
    function initSearchBar(){
        var approveType = window.localStorage.getItem('approve_type');
//        if(approveType == 1){
//            //如果是部门审批
//            $('.section_area').hide();
//            getSectionApprove();
//        }
//        else{
            //如果是领导或者签批处理
            initSectionArea(approveType);
          //  $('.section_area').show();
        //}
    }

    //辅助函数，处理审批类型
    function initApproveType(){
        is_fee = window.localStorage.getItem('is_fee');
        $('#approve_type').empty();
        if(is_fee == 0){
            $('#approve_type').append('<option value="1">出差审批</option>');
            $('#approve_type').append('<option value="2">借款审批</option>');
        }
        else{
            $('#approve_type').append(
                    '<option value="1">差旅报销</option>'+
                    '<option value="2">业务报销</option>');
        }
        $('#approve_type').comboSelect();

    }

    //辅助函数，获取

    //辅助函数，处理部门选择框，分管审批或领导审批的情况
    function initSectionArea(approveType){
        var user = JSON.parse(window.localStorage.getItem('user_info'));
        $('#dataLoad').show();
        $.post(url + 'admin/Leader/LeaderGetLeaderPower.php', {'user_id':user.user_id}, function(data){
            $('#dataLoad').hide();
            console.log(data);
            var t = JSON.parse(data);
            if(t.state == 1){
                $('#section_type').append("<option value=0>全部</option>");
                if(is_fee == 0){ //如果处于业务报销
                    for(var i = 0; i < t.data.length; i++){
                        if(t.data[i].is_approve == 1 && t.data[i].power_level == approveType){
                            var sectionInfo = t.data[i].section_power;
                            for(var j = 0; j < sectionInfo.length; j++){
                                $('#section_type').append("<option value=" + sectionInfo[j].section_id + ">" +
                                        sectionInfo[j].section_name + "</option>");
                            }
                        }
                    }
                }
                else{//如果处于费用报销签批
                    for(var i = 0; i < t.data.length; i++){
                        if(t.data[i].is_fee == 1 && t.data[i].power_level == approveType){
                            var sectionInfo = t.data[i].section_power;
                            for(var j = 0; j < sectionInfo.length; j++){
                                $('#section_type').append("<option value=" + sectionInfo[j].section_id + ">" +
                                        sectionInfo[j].section_name + "</option>");
                            }
                        }
                    }
                }
                $('#section_type').comboSelect();
                getSectionApprove();
            }
            else{
                $.prompt("<h3>服务器忙或网络故障，请稍后再试</h3>", {
                    title: '操作提醒',
                    buttons: {'确定': true},
                    submit: function(e, v, m, f){
                        jumpPage('guide.html')
                    }
                });
            }
        })
    }

    //辅助函数，用于获取当前选择的类型中文信息
    function getSelType(id){
        id = parseInt(id);
        switch(id){
            case 1:
                return (is_fee == 0 ? '出差审批' : '差旅报销');
            case 2:
                return (is_fee == 0 ? '借款审批' : '业务报销');
//            case 3:
//                return '借款审批';

        }
    }


    //辅助函数，用于处理对应信息的审批
    function processApprove(curInfo){
        var type =  parseInt($('#approve_type').val());
        if(is_fee == 0){
            switch(type){
                case 1:
                    processBusinessApprove(curInfo);
                    break;
                case 2:
                    processLoanFeeApprove(curInfo);
                    break;
            }
        }
        else{
            switch(type){
                case 1:
                    processBusinessFeeApprove(curInfo);
                    break;
                case 2:
                    processOrdinaryFeeApprove(curInfo);
                    break;
            }
        }

    }

    //辅助函数，用于处理业务报销审批
    function processOrdinaryFeeApprove(curInfo){
        $('#dataLoad').show();

        $.post(url + 'OrdinaryFee/GetOrdinaryFeeDetail.php', {'appID' : curInfo.fee_id}, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1){
                window.localStorage.setItem('baoxiao', data);
                window.localStorage.setItem('is_history', 0);
                window.localStorage.setItem('approve_state', 1);
                jumpPage('baoxiao_info.html');
            }
            else{
                $.prompt("<h3>服务器忙或网络故障，请稍后再试</h3>", {
                    title: '操作提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }

    //辅助函数，用于处理差旅报销审批信息
    function processBusinessFeeApprove(curInfo){

        $('#dataLoad').show();
       // alert(curInfo.fee_id);
        $.post(url + 'BusinessFee/GetBusinessFeeDetail.php', {'appID' : curInfo.fee_id}, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1) {
             //   window.localStorage.setItem('curItem', data);
                window.localStorage.setItem('approve_state', 1);
                window.localStorage.setItem('is_history', 0);
                window.localStorage.setItem('curFeeInfo', data);
                jumpPage('fee_info.html');
            }
            else{
                $.prompt("<h3>网络或数据库故障，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }

    //辅助函数，用于处理出差审批
    function processBusinessApprove(curInfo){
        $('#dataLoad').show();
        $.post(url+'BusinessApprove/GetBusinessApproveByID.php', {'appID' : curInfo.app_id}, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1){
                window.localStorage.setItem('cur_travel_info', JSON.stringify(t.data[0]));
                window.localStorage.setItem('approve_state', 1);
                jumpPage('bussiness_approval_info.html');
            }
            else{
                $.prompt("<h3>网络或数据库故障，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }

    //辅助函数，用于处理借款审批
    function processLoanFeeApprove(curInfo){
        $('#dataLoad').show();
        $.post(url + 'LoanFee/GetOrdinaryLoanFeeByID.php', {'appID' : curInfo.app_id}, function(data){
            $('#dataLoad').hide();

            var t = JSON.parse(data);
            if(t.state == 1){
                window.localStorage.setItem('curItem', data);
                window.localStorage.setItem('approve_state', 1);
                jumpPage('loan_fee_info.html');
            }
            else{
                $.prompt("<h3>网络或数据库故障，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                });
            }
        })
    }



    //辅助函数，获取当前所有等待审批的信息
    function getSectionApprove(){
        var curItem = JSON.parse(window.localStorage.getItem('user_info'));
        var curDepartID = window.localStorage.getItem('cur_department');
        var sectionID = curItem.department_info[curDepartID].department_id;
        var userID = curItem.user_id;


        var type = $('#approve_type').val();
        var state = window.localStorage.getItem('approve_type');
        var allSection = 0;

        switch(state){
            case '1':
                state = '01';  //待部门审批
                break;
            case '2':
                state = '02';  //待签批审批
                break;
            case '3':
                state = '03';  //待领导审批
                break;
        }


        if($('#section_type').val() != 0){
            sectionID = $('#section_type').val();
            allSection = 0;
        }
        else{
            sectionID = [];
            $('#section_type option').each(function(){
                if($(this).val() != 0){
                    sectionID.push($(this).val());
                }
            });
            allSection = 1;
        }

        var infoURL = url;
        if(is_fee == 0){
            if(type == 1)
                infoURL += 'BusinessApprove/GetSectionBusinessApprove.php';
            if(type == 2){
                infoURL += 'LoanFee/GetSectionOrdinaryLoanFee.php'
            }
        }
        else{
            if(type == 1){
                infoURL += 'BusinessFee/GetSectionFeeApprove.php';
            }
            if(type == 2){
                infoURL += 'OrdinaryFee/GetSectionOrdinaryFeeApprove.php'
            }

        }

        $('#dataLoad').show();
        $.post(infoURL, {'user':userID, 'sectionID': sectionID, 'allSection' : allSection, 'state':state}, function(data){
            var t = JSON.parse(data);
            $('#dataLoad').hide();
            if(t.state == 1){
                if(firstSet){
                    createTablePage(t.total_count);
                    firstSet = false;
                }

                curInfo = t.data;
                createSectionApproveTable(t.data);
            }
        })
    }
    function createSectionApproveTable() {
        $(".info_tr").empty();

        var start = (curPage-1) * rowPerPage;
        var end = (curInfo.length - start)> rowPerPage ? rowPerPage : curInfo.length;

        for (var i = start; i < end; i++) {
            var appType = getSelType(curInfo[i].type) + "-" + getBusinessTypeInfo(curInfo[i].travel_type);
            $('#section_app_info').append("" +
                    "<tr class='tr info_tr'>" +
                        "<td class='td_center'>" + (i + 1) + "</td>" +
                        "<td class='td_center'>" + curInfo[i].user_name + "</td>" +
                        "<td class='td_center'>" + curInfo[i].section_name + "</td>" +
                        "<td class='td_center'>" + appType + "</td>" +
                        "<td class='td_center'>" + curInfo[i].app_time + "</td>" +
                        "<td class='td_center'>" +
                            "<a class='ext_btn shengpi btn_shengpi'><span class='shengpi'></span>审批</a>" +
                        "</td>" +
                    "</tr>");
        }
        $('.shengpi').each(function(index, elem){
            $(this).click(function(){
                var id = $(this).parent().parent().children('td').eq(0).html();
                processApprove(curInfo[id-1]);

            })
        })
    }
    $(function(){
        initApproveType();
        initSearchBar();

        $('#confirm_query').click(function(){
            firstSet = true;
            getSectionApprove();
        });
        clickFirstPage(createSectionApproveTable);
        clickEndPage(createSectionApproveTable);
        clickNextPage(createSectionApproveTable);
        clickPrevPage(createSectionApproveTable);

    })
</script>
</html>